package com.pureut.system.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import com.pureut.common.core.web.controller.BaseController;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.core.web.page.TableDataInfo;

import com.pureut.common.log.annotation.Log;
import com.pureut.common.log.enums.BusinessType;
import com.pureut.system.domain.ChangeApplication;
import com.pureut.system.domain.CompensationApplication;
import com.pureut.system.domain.SupplierAlterHistory;
import com.pureut.system.domain.dto.SupplierListPageDto;
import com.pureut.system.domain.vo.SupplierListVo;
import com.pureut.system.export.SupplierExport;
import com.pureut.system.service.ISupplierListService;
import com.pureut.system.util.DownTemplateUtil;
import com.pureut.system.util.ExcelUtil;
import com.pureut.system.util.FileConstant;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;


/**
 * @Author: pureut
 * @Date: 2024-12-05 17:16:19
 * @Desc: 供应商名录Controller
 */
@RestController
@RequestMapping("/supplierList")
public class SupplierListController extends BaseController {

    /**
     * 供应商名录Service
     */
    @Resource
    private ISupplierListService supplierListService;

    /**
     * 查询供应商名录列表
     * @param: params 参数列表
     * @return: Json工具类对象
     */
    //    @SaCheckPermission("supplierList-list")
    @GetMapping("/list")
    public TableDataInfo list( SupplierListVo supplierListVo) {
        startPage();
        return getDataTable(supplierListService.queryPage(supplierListVo));
    }



    /**
     * 获取供应商名录详细信息
     * @param: id 供应商名录主键
     * @return: Json工具类对象
     */
    //    @SaCheckPermission("supplierList-query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(supplierListService.getInfoById(id));
    }

    /**
     * 新增供应商名录
     * @param: supplierList 供应商名录对象
     * @return: Json工具类对象
     */
    //    @SaCheckPermission("supplierList-add")
    @PostMapping
    public AjaxResult add(@RequestBody SupplierListVo supplierListVo) {
        return toAjax(supplierListService.save(supplierListVo));
    }

    /**
     * 修改供应商名录
     * @param: supplierList 供应商名录对象
     * @return: Json工具类对象
     */
    //    @SaCheckPermission("supplierList-edit")
    @PutMapping
    public AjaxResult edit(@RequestBody SupplierListVo supplierListVo) {
        return toAjax(supplierListService.updateById(supplierListVo));
    }

    /**
     * 删除供应商名录
     * @param: id 供应商名录主键
     * @return: Json工具类对象
     */
    //    @SaCheckPermission("supplierList-remove")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(supplierListService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 供应商导出
     */
    @PostMapping("/export")
    public AjaxResult export(HttpServletResponse response, SupplierListVo supplierListVo) throws IOException
    {
        List<SupplierListPageDto> infoExport = supplierListService.getInfoExport(supplierListVo);
        ExcelUtil<SupplierListPageDto> excelUtil = new ExcelUtil<SupplierListPageDto>(SupplierListPageDto.class);
        excelUtil.excelWriter(response, infoExport, "导入供应商信息.xlsx");
        return AjaxResult.success();
    }

    /**
     * 供应商导入模板下载
     * @param response
     * @throws IOException
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        DownTemplateUtil.downTemplate(response, FileConstant.SUPPLIER_IMPORT);
    }

    /**
     * 供应商模板导入
     * @param file
     * @return
     * @throws Exception
     */
    @Log(title = "供应商模板导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<SupplierExport> excelUtil = new ExcelUtil<SupplierExport>(SupplierExport.class);
        List<SupplierExport> coCustomer = excelUtil.importExcel(file.getInputStream());
        return AjaxResult.success(supplierListService.importMouldData(coCustomer));
    }

    /**
     * 供应商变更历史
     */
    @GetMapping("/getAlterHistoryList")
    public TableDataInfo getAlterHistoryList(String supplierId){
        startPage();
        return getDataTable(supplierListService.getAlterHistoryList(supplierId));
    }

    /**
     * 供方信息保存
     */
    @PostMapping("/saveChangeApplication")
    public AjaxResult saveChangeApplication (@RequestBody ChangeApplication changeApplication){
       return toAjax(supplierListService.saveChangeApplication(changeApplication));
    }

    /**
     * 查询供方变更前信息
     */
    @GetMapping("/getBeforeSuppler")
    public AjaxResult getBeforeSuppler(SupplierAlterHistory supplierAlterHistory){
        return AjaxResult.success(supplierListService.getBeforeSuppler(supplierAlterHistory));
    }

    /**
     * 供方信息变更
     *
     */
    @PutMapping("/updateSupplierHis")
    public AjaxResult updateSupplierHis(@RequestBody SupplierAlterHistory supplierAlterHistory){
        return toAjax(supplierListService.updateSupplierHis(supplierAlterHistory));
    }

    /**
     * 供应商索赔管理
     */
    @PostMapping("/supplierClaim")
    public AjaxResult supplierClaim(@RequestBody CompensationApplication compensationApplication){
        return toAjax(supplierListService.supplierClaim(compensationApplication));
    }
}
